<template>
  <div class="col card p10">
    <label class="title">电子秤校准</label>
    <div class="row_center mt">
      <label class="less">电子秤:</label>
      <el-select v-model="ctx.target" style="width: 130px;">
        <el-option
          :value="FRONT"
          :label="FRONT"
        />
        <el-option
          :value="BACK"
          :label="BACK"
        />
      </el-select>
    </div>
    <div class="row_center mt">
      <label class="less">砝码重量:</label>
      <el-input size="small" v-model="ctx.weight" style="width: 80px;"/>
    </div>
    <el-button class="mt" @click="onClickAdj" type="primary">校准</el-button>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { dialog } from '../../service/ui';
import { adj } from '../../service/weight_cmd';

const FRONT = 'front电子秤';
const BACK = 'back电子秤';

interface Ctx {
  target: string,
  weight: string,
}

const ctx = reactive<Ctx>({
  target: FRONT,
  weight: '0'
});

async function onClickAdj() {

  try {
    let target = 1;
    if (ctx.target == BACK) {
      target = 2;
    }
    const w = parseInt(ctx.weight);
    await adj(target, w);
    dialog.succ('校准成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

</script>
